(function($) {
    var printAreaCount = 0;
    $.fn.printArea = function() {
        var ele = $(this);
        var idPrefix = "printArea_";
        removePrintArea(idPrefix + printAreaCount);
        printAreaCount++;
        var iframeId = idPrefix + printAreaCount;
        var iframeStyle = 'position:absolute;width:0px;height:0px;left:-500px;top:-500px;';
        iframe = document.createElement('IFRAME');
        $(iframe).attr({
            style: iframeStyle,
            id: iframeId
        });
        document.body.appendChild(iframe);
        var doc = iframe.contentWindow.document;
        doc.write('<title>ggggggg</title>');
        $(document).find("link").filter(function() {
            return $(this).attr("rel").toLowerCase() == "stylesheet";
        }).each(
                function() {
                    doc.write('<link type="text/css" rel="stylesheet" href="'
                            + $(this).attr("href") + '" >');
                });
        doc.write('<div class="' + $(ele).attr("class") + '">' + $(ele).formhtml()
                + '</div>');
        doc.close();
        console.log(doc);
        var frameWindow = iframe.contentWindow;
        frameWindow.close();
        frameWindow.focus();
        frameWindow.print();
    }
    var removePrintArea = function(id) {
        $("iframe#" + id).remove();
    };
})(jQuery);

(function($) {
    var oldHTML = $.fn.html;
    $.fn.formhtml = function() {
        if (arguments.length)
            return oldHTML.apply(this, arguments);
        $("input", this).each(function() {
            this.setAttribute('value', this.value);
        });
        $("textarea,button", this).each(function() {
            $(this).text(this.value);
        });

        $(":radio,:checkbox", this).each(function() {
            if (this.checked)
                this.setAttribute('checked', 'checked');
            else
                this.removeAttribute('checked');
        });
        $("option", this).each(function() {
            if (this.selected)
                this.setAttribute('selected', 'selected');
            else
                this.removeAttribute('selected');
        });
        return oldHTML.apply(this);
    };
})(jQuery);